package main

import (
	"context"
	"database/sql"
	"net"
	"strings"

	"github.com/go-sql-driver/mysql"
	"golang.org/x/crypto/ssh"
)

const sshNetName = "tcpSSH"

func SSHOpen(client *ssh.Client, driver, source string) (*sql.DB, error) {
	mysql.RegisterDialContext(sshNetName, func(ctx context.Context, addr string) (conn net.Conn, err error) {
		return client.Dial("tcp", addr)
	})
	source = strings.Replace(source, "@tcp", "@"+sshNetName, 1)
	return sql.Open(driver, source)
}
